
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QTabWidget)
from PyQt5.QtGui import QFont
from gps import GPSInfoExtractor
from report import WordGenerator
from imageAnnotator import ImageAnnotationWidget
import traceback

import time

from license_manager import check_license


def handle_exception(exc_type, exc_value, exc_traceback):
    """捕获所有未处理的异常"""
    error_msg = ''.join(traceback.format_exception(exc_type, exc_value, exc_traceback))
    with open('error.log', 'a') as f:
        f.write("=" * 50 + "\n")
        f.write(f"崩溃时间: {time.ctime()}\n")
        f.write(error_msg)
        f.write("\n" + "=" * 50 + "\n")

    # 显示错误对话框（可选）
    from PyQt5.QtWidgets import QMessageBox
    msg = QMessageBox()
    msg.setIcon(QMessageBox.Critical)
    msg.setText("程序发生错误")
    msg.setInformativeText(str(exc_value))
    msg.setWindowTitle("错误")
    msg.exec_()

    sys.__excepthook__(exc_type, exc_value, exc_traceback)



class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        # 检查许可证
        if not check_license():
            sys.exit(1)  # 注册失败退出

        self.setWindowTitle("无人机照片应用软件")
        self.setGeometry(100, 100, 1200, 800)

        # 创建标签页
        self.tabs = QTabWidget()

        # 添加各功能模块
        self.gps_extractor = GPSInfoExtractor()
        self.word_generator = WordGenerator()
        self.image_annotator = ImageAnnotationWidget()

        self.tabs.addTab(self.gps_extractor, "GPS信息提取")
        self.tabs.addTab(self.image_annotator, "图片标注")
        self.tabs.addTab(self.word_generator, "Word文档生成")
        # 设置中央部件
        self.setCentralWidget(self.tabs)



if __name__ == '__main__':
    sys.excepthook = handle_exception

    try:
        app = QApplication(sys.argv)

        # 设置中文显示
        font = QFont("Microsoft YaHei", 10)
        app.setFont(font)

        main_window = MainWindow()
        main_window.show()
        sys.exit(app.exec_())
    except Exception as e:
        handle_exception(type(e), e, e.__traceback__)


